package com.leetcode;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * 380. O(1) 时间插入、删除和获取随机元素
 * 使用HashSet
 *
 * @author fy
 * @date 2022/4/13 8:59
 */
public class Solution380 {

    public class RandomizedSet {

        private Set<Integer> set;

        public RandomizedSet() {
            set = new HashSet<>();
        }

        public boolean insert(int val) {
            return set.add(val);
        }

        public boolean remove(int val) {
            return set.remove(val);
        }

        public int getRandom() {
            Random random = new Random();
            return (Integer)set.toArray()[random.nextInt(set.size())];
        }
    }

}
